﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFWS.XHandler.Core;
using JKF.Substructure;
using JKF.Entities.Model;

namespace JKF.HandlerLayer.Dic
{
    public partial class Dic_AreaHandler : AHttpHandler
    {
        public override void ProcessRequest()
        {
            //所有错误和异常将抛出异常.正常执行程序则在方法则返回数据
            try
            {
                this.Manager(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                //写日志
                AppLogger.Log(ex.Message, ex);
            }
        }
        /// <summary>
        /// 获取省/市 数据
        /// </summary>
        public void GetDIC_AREAProvince()
        {
           
            try
            { 
                var oList = new JKF.BusinessLayer.BLL.bllDIC_AREA().GetDIC_AREAProvince();
                if (oList == null)
                {
                    Response.Write(new ResponseInf { Success = false, Message = "没有权限！" }.ToJson());
                    return;
                }
                Response.Write(new ResponseInf
                {
                    Data = (from a in oList select new { value = a.AREA_CODE, text = a.NAME }).ToList(),
                    Success = true
                }.ToJson());
            }
            catch (Exception exp)
            {
                Response.Write(new ResponseInf { Success = false, Message = exp.Message }.ToJson());
            }
        }
        /// <summary>
        /// 取得所有区域
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public void GetArea()
        {
            string code = Request["code"];
            var sb = new StringBuilder();
            var oList = new JKF.BusinessLayer.BLL.bllDIC_AREA().GetArea(code);
            foreach (DIC_AREAinfo item in oList)
            {
                sb.AppendFormat("<option value=\"{0}\">{1}</option>", item.AREA_CODE, item.NAME);
            }
            Utils.ToClient(false, sb.ToString());
        }
        /// <summary>
        /// 获取第二级数据（市/区）
        /// </summary>
        public void GetAreaJson()
        {
            try
            {
                string code = Request["code"];
                var sb = new StringBuilder();
                var oList = new JKF.BusinessLayer.BLL.bllDIC_AREA().GetArea(code);
                Response.Write(new ResponseInf
                                   {
                                       Data = (from a in oList select new { value = a.AREA_CODE, text = a.NAME }).ToList(),
                                       Success = true
                                   }.ToJson());
            }
            catch (Exception exp)
            {
                Response.Write(new ResponseInf { Success = false, Message = exp.Message }.ToJson());
            }
        }
    }
}
